import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import swal, { SweetAlertType } from 'sweetalert2';

@Component({
  selector: 'app-c-bullet-tool',
  templateUrl: './c-bullet-tool.component.html',
  styleUrls: ['./c-bullet-tool.component.less']
})
export class CBulletToolComponent implements OnInit {

    @Output() onChange = new EventEmitter<any>();
    @Output() onChangeTxt = new EventEmitter<any>();
    @Output() onDelete = new EventEmitter<any>();

    targetData;

    @Input() rightData;

    @Input() comeList;

    set rightText(value){
        this.rightData.text = value;
        this.onChangeTxt.emit(this.rightData);
    }

    get rightText(){
        if (!this.rightData.text) {
            return '请输入题目';
        } else {
            return this.rightData.text;
        }
    }

    _rightStyleX;
    @Input()
    set rightStyleX(value){
        this._rightStyleX = value;
        this.emitXYWH();
    }

    get rightStyleX(){
        return this._rightStyleX;
    }

    _rightStyleY;
    @Input()
    set rightStyleY(value){
        this._rightStyleY = value;
        this.emitXYWH();
    }

    get rightStyleY(){
        return this._rightStyleY;
    }

    _rightStyleW;
    @Input()
    set rightStyleW(value){
        this._rightStyleW = value;
        this.emitXYWH();
    }

    get rightStyleW(){
        return this._rightStyleW;
    }

    _rightStyleH;
    @Input()
    set rightStyleH(value){
        this._rightStyleH = value;
        this.emitXYWH();
    }

    get rightStyleH(){
        return this._rightStyleH;
    }



    constructor() { }

    ngOnInit() {

    }

    emitXYWH() {
        this.onChange.emit({
            x: this._rightStyleX,
            y: this._rightStyleY,
            width: this._rightStyleW,
            height: this._rightStyleH
        });
    }

    fileChange($event) {
        const file: File = $event.target.files[0];
        const myReader: FileReader = new FileReader();
        const that = this;
        myReader.onloadend = (loadEvent: any) => {
            that.rightData.src = loadEvent.target.result;
        };
        myReader.readAsDataURL(file);
    }

    deleteComponent() {

        this.onDelete.emit();
    }

    save() {
        swal(`保存成功`, '', 'info');
    }
}
